<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      :label-position="labelPosition"
      label-width="100px"
    >
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="合同编号">
            <el-input v-model="form.contractNo" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="合同期限(月)" prop="deadline">
            <el-input maxlength="11" v-model.number="form.deadline" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="合同开始时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.startDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="合同结束时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.endDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="试用期限(月)" prop="trialDeadline">
            <el-input v-model.number="form.trialDeadline" maxlength="11" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="试用开始日期">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.trialStartDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="试用结束日期">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.trialEndDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="经济补偿金" prop="compensation">
            <el-input v-model.number="form.compensation" :style="inputWidth" maxlength="10" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="赔偿金" prop="damage">
            <el-input v-model.number="form.damage" maxlength="10" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="附件" prop="attachment">
            <el-upload
              accept=".jpg, .png, .jpeg, .gif"
              action="https://jsonplaceholder.typicode.com/posts/"
              :multiple="true"
              :on-preview="handlePreview"
              :file-list="this.fileList"
              :on-remove="handleRemove"
              :http-request="customRequest"
            >
              <el-button>
                <i class="el-icon-upload" />上传
              </el-button>
            </el-upload>
            <!-- <el-input v-model.number="form.damage" maxlength="10" :style="inputWidth" /> -->
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="备注">
            <el-input type="textarea" :rows="2" v-model="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
    <preview-modal ref="previewModal" />
  </el-dialog>
</template>

<script>
import { updateUserContract, createUserContract } from '@/api/archives'
import { getSession, openNotification, validateNum } from '@/utils/util'
import { saveOnlyUploads } from '@/api/supplier'
import PreviewModal from '@/components/PreviewModal'

export default {
  components: {
    PreviewModal
  },
  data() {
    return {
      visible: false,
      form: {},
      labelPosition: 'right',
      inputWidth: {
        width: '100%'
      },
      rules: {
        deadline: validateNum,
        trialDeadline: validateNum,
        compensation: validateNum,
        damage: validateNum
      },
      isEdit: false,
      userRow: null,
      fileList: []
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
  },
  methods: {
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          this.$set(this.form, 'userId', this.userRow.userId)
          if (this.isEdit) {
            res = await updateUserContract(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserContract(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    // 预览图片
    handlePreview(file) {
      console.log('file', file)
      let arr = []
      this.fileList.map(v => {
        arr.push(v.url)
      })
      this.$refs.previewModal.show(arr)
      // console.log('tableData', this.itemArr[index].tableData, ind, one)
    },
    // 删除图片事件处理
    handleRemove(file, fileList, row, item) {
      console.log(file, fileList)
      this.fileList = [...fileList]
    },
    // 上传图片事件处理
    customRequest(info) {
      // const _this = this
      console.log('customRequest', info)
      const formData = new FormData()
      formData.append('file', info.file)
      // 调方法，post，body 放入 formData
      // this.fileList = []

      saveOnlyUploads(formData).then(res => {
        if (res.data !== null) {
          const { data } = res
          this.fileList.push(data)
          this.fileList[this.fileList.length - 1].url = data.fileUrl + data.fileName
          this.fileList[this.fileList.length - 1].savePath = data.fileUrl + data.fileName
          this.fileList[this.fileList.length - 1].name = data.uploadTitle

          console.log('fileList', this.fileList)

          let arr = []
          this.fileList.map(v => {
            arr.push(v.uploadId)
          })

          this.$set(this.form, 'attachment', arr.join(','))
        }
      })
    },
    edit(bool, row) {
      console.log('row', row)
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, row)
    }
  }
}
</script>